package com.gy.hsim.bservice.userGroupingManagement.service.impl;

import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.common.util.GuidAgent;
import com.gy.hsim.bservice.framework.common.util.HTTPSend;
import com.gy.hsim.bservice.framework.mapper.GroupMapper;
import com.gy.hsim.bservice.userGroupingManagement.bean.Group;
import com.gy.hsim.bservice.userGroupingManagement.bean.GroupVerify;
import com.gy.hsim.bservice.userGroupingManagement.bean.Member;
import com.gy.hsim.bservice.userGroupingManagement.bean.Notice;
import com.gy.hsim.bservice.userGroupingManagement.bean.PushGroupMsgBody;
import com.gy.hsim.bservice.userGroupingManagement.service.GroupService;

/**
 * @ClassName: GroupServiceImpl
 * @Description:  组群业务服务实现类
 * @author Lee
 * @date 2015-12-11 上午09:37:19
 */
@Service("GroupService")
public class GroupServiceImpl implements GroupService {
	@Autowired
	private GroupMapper groupMapper;
	
	@Autowired
	private SystemConfig config;
	
	/**群推送消息组ID业务代码  begin*/
	private static final String joinGroupReqDesGroupId = "HDQXX10001";     //请求加入群
	private static final String acceptJoinGroupDesGroupId = "HDQXX10002";  //同意加入群
	private static final String refuseJoinGroupDesGroupId = "HDQXX10003";  //拒绝加入群
	private static final String removeGroupMemberDesGroupId = "HDQXX10004";  //把群成员踢出群
	private static final String memberLeaveGroupDesGroupId = "HDQXX10005";  //群成员退出群
	private static final String setManagerRoleDesGroupId = "HDQXX10006";  //设置管理员
	private static final String cancelManagerRoleDesGroupId = "HDQXX10007";  //取消管理员
	private static final String cancelGroupDesGroupId = "HDQXX10008";  //解散群
	
	
	/**群推送消息组ID业务代码  end*/
	
	@Override
	@Transactional
	public int addGroup(Group group) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		Member member = new Member();
		member.setMemberGroupId(group.getGroupId());
		member.setMemberUserId(group.getGroupCreator());
		member.setMemberVerificationBy(group.getGroupCreator());
		member.setMemberRole("H");
		member.setMemberUpdateBy(group.getGroupCreator());
		groupMapper.addUserIntoGroup(member);  //群主创建群的时候也把自己添加到群成员信息中
		groupMapper.addGroup(group);
		return successCode;
	}
	
	@Override
	public List<Group> queryGroupIdByUserId(Group group) throws Exception {
		// TODO Auto-generated method stub
		List<Group> groupList = new ArrayList<Group>();
		groupList = groupMapper.queryGroupIdByUserId(group);
		return groupList;
	}

	@Override
	public int updateGroupInfo(Group group) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		groupMapper.updateGroupInfo(group);				
		return successCode;
	}

	@Override
	public List<Group> queryGroupInfoByGroupParam(Group group) throws Exception {
		// TODO Auto-generated method stub
		List<Group> groupList = new ArrayList<Group>();
		groupList = groupMapper.queryGroupInfoByGroupParam(group);
		return groupList;
	}
	
	@Override
	public List<Group> queryGroupInfoByGroupId(Group group) throws Exception {
		// TODO Auto-generated method stub
		List<Group> groupList = new ArrayList<Group>();
		groupList = groupMapper.queryGroupInfoByGroupId(group);
		return groupList;
	}
		
	@Override
	public List<Group> queryGroupInfoByUserId(Group group) throws Exception {
		// TODO Auto-generated method stub
		List<Group> groupList = new ArrayList<Group>();
		groupList = groupMapper.queryGroupInfoByUserId(group);
		return groupList;
	}

	@Override
	@Transactional
	public int deleteGroup(Group group) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		Member member= new Member();
		member.setMemberGroupId(group.getGroupId());
		groupMapper.deleteAllMemberInGroup(member);
		groupMapper.deleteGroup(group);				
		return successCode;
	}

	@Override
	@Transactional
	public int addUserIntoGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		//把成员加入到群成员表中
		groupMapper.addUserIntoGroup(member);
		//在验证信息表中更新验证状态为同意
		GroupVerify groupVerify = new GroupVerify();
		groupVerify.setVerifyId(member.getMemberVerifyId());
		groupVerify.setVerifyBy(member.getMemberVerificationBy());
		groupVerify.setVerifyResult("Y");
		groupVerify.setVerifyMsg("同意");
		updateGroupVerifyInfo(groupVerify);
		return successCode;
	}

	@Override
	public int refuseUserJoinGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		//在验证信息表中更新验证状态为同意
		GroupVerify groupVerify = new GroupVerify();
		groupVerify.setVerifyId(member.getMemberVerifyId());
		groupVerify.setVerifyBy(member.getMemberVerificationBy());
		groupVerify.setVerifyResult("N");
		groupVerify.setVerifyMsg("拒绝");
		updateGroupVerifyInfo(groupVerify);
		return successCode;
	}
	
	@Override
	public int deleteAllMemberInGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		groupMapper.deleteAllMemberInGroup(member);				
		return successCode;
	}

	@Override
	public int updateMemberInfo(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		groupMapper.updateMemberInfo(member);
		return successCode;
	}

	@Override
	public List<Group> queryGroupInfoUserJoin(Member member) throws Exception {
		// TODO Auto-generated method stub
		List<Group> groupList = new ArrayList<Group>();
		groupList = groupMapper.queryGroupInfoUserJoin(member);
		return groupList;
	}
	
	@Override
	public List<Member> queryMemberInfoByGroupId(Member member)
			throws Exception {
		// TODO Auto-generated method stub
		List<Member> memberList = new ArrayList<Member>();
		memberList = groupMapper.queryMemberInfoByGroupId(member);
		return memberList;
	}
	
	@Override
	public List<Member> queryMemberInfoAfterGroupDelete(Member member)
			throws Exception {
		// TODO Auto-generated method stub
		List<Member> memberList = new ArrayList<Member>();
		memberList = groupMapper.queryMemberInfoAfterGroupDelete(member);
		return memberList;
	}
	
	@Override
	public List<Member> queryAllManagerInfoByGroupId(Member member)
			throws Exception {
		// TODO Auto-generated method stub
		List<Member> memberList = new ArrayList<Member>();
		memberList = groupMapper.queryAllManagerInfoByGroupId(member);
		return memberList;
	}

	@Override
	public String queryUserSilenceFlag(Member member) throws Exception {
		// TODO Auto-generated method stub
		String  silenceFlag = "";
		silenceFlag = groupMapper.queryUserSilenceFlag(member);
		return silenceFlag;
	}

	@Override
	public String queryUserRoleInGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		String  userRole = "";
		userRole = groupMapper.queryUserRoleInGroup(member);
		if(userRole == null){
			userRole = "";
		}
		return userRole;
	}

	@Override
	public int queryIfUserInGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.queryIfUserInGroup(member);
		return successCode;
	}

	@Override
	public int deleteMemberInGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.deleteMemberInGroup(member);
		return successCode;
	}

	@Override
	public int memberLeaveGroup(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.memberLeaveGroup(member);
		return successCode;
	}
	
	@Override
	@Transactional
	public int memberTransferHost(Member member) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;		
		groupMapper.changeHostRole(member);   //更新群主为普通成员
		Member memberOfuser = new Member();
		memberOfuser.setMemberGroupId(member.getMemberGroupId());
		memberOfuser.setMemberUserId(member.getMemberUserId());
		memberOfuser.setMemberUpdateBy(member.getMemberUpdateBy());
		groupMapper.changeMemberRole(memberOfuser);  //更新群成员为群主
		Group group = new Group();
		group.setGroupId(member.getMemberGroupId());
		group.setGroupHost(member.getMemberUserId());
		groupMapper.changeHost(group);
		return successCode;
	}

	@Override
	public int addGroupVerifyInfo(GroupVerify groupVerify) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.addGroupVerifyInfo(groupVerify);
		return successCode;
	}

	@Override
	public int updateGroupVerifyInfo(GroupVerify groupVerify) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.updateGroupVerifyInfo(groupVerify);
		return successCode;
	}

	@Override
	public List<GroupVerify> queryUnverifiedInfoList(GroupVerify groupVerify)
			throws Exception {
		// TODO Auto-generated method stub
		List<GroupVerify> groupVerifyList = new ArrayList<GroupVerify>();
		groupVerifyList = groupMapper.queryUnverifiedInfoList(groupVerify);
		return groupVerifyList;
	}

	@Override
	public List<GroupVerify> queryVerifyInfoByVerifyId(GroupVerify groupVerify)
			throws Exception {
		// TODO Auto-generated method stub
		List<GroupVerify> groupVerifyList = new ArrayList<GroupVerify>();
		groupVerifyList = groupMapper.queryVerifyInfoByVerifyId(groupVerify);
		return groupVerifyList;
	}
	
	@Override
	public String getComparetDateStr(Date date,int dataDiff) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, dataDiff);
		date = calendar.getTime();		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
		String dateStr = dateFormat.format(date);		
		return dateStr;
	}

	@Override
	public int checkVerifyInfoOutDate(String verifyReqDateStr) throws Exception {
		// TODO Auto-generated method stub
		int result = 0;
		int expirationDate = Integer.parseInt(config.getOverdueDate());     //有效期,在配置文件中获取
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();				
		String compareStr = getComparetDateStr(now, 0-expirationDate);  //获取比较的时间字符串
		Date verifyReqDate = dateFormat.parse(verifyReqDateStr);
		Date compareStrDate = dateFormat.parse(compareStr);
		result = verifyReqDate.compareTo(compareStrDate);		
		return result;
	}

	@Override
	public int addNotice(Notice notice) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.addNotice(notice);
		return successCode;
	}

	@Override
	public int setHisNoticeUnused(Notice notice) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		successCode = groupMapper.setHisNoticeUnused(notice);
		return successCode;
	}

	@Override
	public List<Notice> queryNoticeInfo(Notice notice) throws Exception {
		// TODO Auto-generated method stub
		List<Notice> noticeList = new ArrayList<Notice>();
		noticeList = groupMapper.queryNoticeInfo(notice);
		return noticeList;
	}

	/*@Override
	public String setGroupId(String groupCreator, String dateStr)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder groupId = new StringBuilder();
		dateStr = dateStr.substring(dateStr.length()-5,dateStr.length());
		for(int i = 0; i < groupCreator.length(); i++){
			groupId.append(groupCreator.charAt(i));
			if(i%2 == 1){
				String subDateStr = String.valueOf(dateStr.charAt(i/2));
				groupId.append(subDateStr);
			}
		}
		return String.valueOf(groupId);
	}*/
	
	@Override
	public String setGroupId(String groupCreator) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder groupId = new StringBuilder();
		List<Group> groupList = new ArrayList<Group>();
		Group group = new Group();
		String groupCreatorSub = groupCreator.substring(0,5);
		group.setGroupCreator(groupCreatorSub);
		groupList = queryGroupIdByUserId(group);
		
		int randNum = 0;
		String randNumStr ="";
		boolean numFlag = false;
				
		do {
			numFlag = false;
			//从1到999999取一个随机数	
			Random rand = new Random();
			randNum = rand.nextInt(999999);
			//不满6位的前面补'0'
			randNumStr = String.valueOf(randNum);
			int randNumStrlen = 6-randNumStr.length();
			for(int i = 0;i < randNumStrlen;i++){
				randNumStr = '0' + randNumStr;
			}
			//随机数后6位不等于查询出来的已有的群ID后6位,把前面截取出来的资源号前5位拼接随机数（6位）形成新的群ID，使得群ID为11位且唯一
			for(Group groupInList : groupList){
				if(groupInList.getGroupId().substring(5).equals(randNumStr)){
					numFlag = true;
					break;
				}
			}	
		} while (numFlag);
		
		groupId.append(groupCreatorSub).append(randNumStr);
		return String.valueOf(groupId);
	}

	@Override
	public boolean checkNumberLimit(int checkType, Object object)
			throws Exception {
		// TODO Auto-generated method stub
		boolean flag = true;		 
		switch (checkType) {
		case 1:
			Member member1 = (Member)object;
			int groupMemberLimit = Integer.parseInt(config.getGroupMemberLimit());
			int groupMemberCount = groupMapper.getMemberCount(member1);
			if(groupMemberCount >= groupMemberLimit){
				flag = false;
			}
			break;
		case 2:
			Group group = (Group) object;
			int groupCreateLimit = Integer.parseInt(config.getGroupCreateLimit());
			int groupCreateCount = groupMapper.getCreateGroupCount(group);
			if(groupCreateCount >= groupCreateLimit){
				flag = false;
			}
			break;
		case 3:
			Member member3 = (Member)object;
			int groupJoinLimit = Integer.parseInt(config.getGroupJoinLimit());
			int groupJoinCount = groupMapper.getJoinGroupCount(member3);
			if(groupJoinCount >= groupJoinLimit){
				flag = false;
			}
			break;
		case 4:
			Member member4 = (Member)object;
			int groupManagerLimit = Integer.parseInt(config.getGroupManagerLimit());
			int groupManagerCount = groupMapper.getGroupManagerCount(member4);
			if(groupManagerCount >= groupManagerLimit){
				flag = false;
			}
			break;
		default:
			flag = false;
			break;
		}		
		return flag;
	}

	@Override
	public void groupPushMsg(Member member,int pushType)
			throws Exception {
		// TODO Auto-generated method stub
		PushGroupMsgBody pushGroupMsgBody = setPushGroupMsgBody(member, pushType);
		//组装消息体
		JSONObject msgHeaderJson = new JSONObject();  //消息头head部分
		msgHeaderJson.element("accessID", "");
		msgHeaderJson.element("accessPwd", "");
		msgHeaderJson.element("appKey", "");
		msgHeaderJson.element("desGroupId", exchangeNull(pushGroupMsgBody.getDesGroupId()));
		msgHeaderJson.element("desUids", exchangeNull(pushGroupMsgBody.getDesUids()));
		msgHeaderJson.element("desName", exchangeNull(pushGroupMsgBody.getDesName()));
		msgHeaderJson.element("groupName", exchangeNull(pushGroupMsgBody.getGroupName()));
		msgHeaderJson.element("expires", "");
		msgHeaderJson.element("pushType", "5");
		msgHeaderJson.element("sign", "");
		msgHeaderJson.element("srcSysType", "3");
		msgHeaderJson.element("srcUid", exchangeNull(pushGroupMsgBody.getSrcUid()));
		msgHeaderJson.element("srcName", exchangeNull(pushGroupMsgBody.getSrcName()));
		msgHeaderJson.element("terminalType", "1,2");
		//获取timeStamp
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
		String dateStr = dateFormat.format(now);
		msgHeaderJson.element("timeStamp", dateStr);
		//获取transactionID
		String uuid = UUID.randomUUID().toString();		
		msgHeaderJson.element("transactionID", uuid);
		msgHeaderJson.element("version", "1.0");
		msgHeaderJson.element("vertifyId", exchangeNull(pushGroupMsgBody.getVertifyId()));
		
		
		
		JSONObject msgBodyContentJson = new JSONObject();  //消息内容content部分
		msgBodyContentJson.element("msg_code", "205");
		msgBodyContentJson.element("msg_content", exchangeNull(pushGroupMsgBody.getMsg_content()));
		//获取msg_id
		GuidAgent uidAgent = new GuidAgent(2);
		Random rand = new Random();
		int randNum = rand.nextInt(9);
		String msg_id = String.valueOf(uidAgent.nextId()*10 + randNum);
		msgBodyContentJson.element("msg_id", msg_id);
		msgBodyContentJson.element("msg_subject", exchangeNull(pushGroupMsgBody.getMsg_subject()));
		msgBodyContentJson.element("msg_type", "1");
		msgBodyContentJson.element("sub_msg_code", pushGroupMsgBody.getSub_msg_code());
				
		JSONObject msgBodyJson = new JSONObject();  //整合消息体
		msgBodyJson.element("content","\"" + msgBodyContentJson.toString() + "\"");
		
		JSONObject msgJson = new JSONObject();
		msgJson.element("body", msgBodyJson.toString());
		msgJson.element("header", msgHeaderJson.toString());
		
		String msgJsonStr = msgJson.toString();				
		String psiUrl = config.getPsiUrl();  //获取网关地址
		
		HTTPSend send = new HTTPSend();
		send.sendRequest(msgJsonStr, psiUrl);  //发送推送消息
		
	}
				
	@Override
	public PushGroupMsgBody setPushGroupMsgBody(Member member, int pushType)
			throws Exception {
		PushGroupMsgBody pushGroupMsgBody = new PushGroupMsgBody();
		switch (pushType) {
		case 1:  //请求加入群
			pushGroupMsgBody.setMsg_content(member.getMemberReqInfo());
			pushGroupMsgBody.setMsg_subject("请求加入群");
			
			pushGroupMsgBody.setDesGroupId(joinGroupReqDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUserId());
			pushGroupMsgBody.setSrcName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20501");
			break;
		case 2:  //同意加入群
			pushGroupMsgBody.setMsg_content("同意加入群");
			pushGroupMsgBody.setMsg_subject("同意加入群");
			
			pushGroupMsgBody.setDesGroupId(acceptJoinGroupDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberVerificationBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setDesUids("c_" + member.getMemberUserId());
			pushGroupMsgBody.setDesName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20502");
			break;
		case 3:  //拒绝加入群
			pushGroupMsgBody.setMsg_content("拒绝加入群");
			pushGroupMsgBody.setMsg_subject("拒绝加入群");
			
			pushGroupMsgBody.setDesGroupId(refuseJoinGroupDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberVerificationBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setDesUids("c_" + member.getMemberUserId());
			pushGroupMsgBody.setDesName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20503");
			break;
		case 4:  //踢出群
			pushGroupMsgBody.setMsg_content("被踢出群");
			pushGroupMsgBody.setMsg_subject("被踢出群");
			
			pushGroupMsgBody.setDesGroupId(removeGroupMemberDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUpdateBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setDesUids("c_" + member.getMemberUserId());
			pushGroupMsgBody.setDesName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20504");
			break;
		case 5:  //退出群
			pushGroupMsgBody.setMsg_content("退出群");
			pushGroupMsgBody.setMsg_subject("退出群");
			
			pushGroupMsgBody.setDesGroupId(memberLeaveGroupDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUserId());
			pushGroupMsgBody.setSrcName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20505");
			break;
		case 6:  //增加管理员
			pushGroupMsgBody.setMsg_content("增加管理员");
			pushGroupMsgBody.setMsg_subject("增加管理员");
			
			pushGroupMsgBody.setDesGroupId(setManagerRoleDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUpdateBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setDesUids("c_" + member.getMemberUserId());
			pushGroupMsgBody.setDesName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20506");
			break;
		case 7:  //取消管理员
			pushGroupMsgBody.setMsg_content("取消管理员");
			pushGroupMsgBody.setMsg_subject("取消管理员");
			
			pushGroupMsgBody.setDesGroupId(cancelManagerRoleDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUpdateBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setDesUids("c_" + member.getMemberUserId());
			pushGroupMsgBody.setDesName(member.getMemberNickName());
			pushGroupMsgBody.setSub_msg_code("20507");
			break;
		case 8:  //解散群
			pushGroupMsgBody.setMsg_content("解散群");
			pushGroupMsgBody.setMsg_subject("解散群");
			
			pushGroupMsgBody.setDesGroupId(cancelGroupDesGroupId + "_" + member.getMemberGroupId());
			pushGroupMsgBody.setGroupName(member.getMemberGroupName());
			pushGroupMsgBody.setSrcUid("c_" + member.getMemberUpdateBy());
			pushGroupMsgBody.setSrcName(member.getMemberManagerNickName());
			pushGroupMsgBody.setSub_msg_code("20508");
			break;	
		default:
			break;
		}
		// TODO Auto-generated method stub
		return pushGroupMsgBody;
	}

	//处理字符串，如果为NULL，则返回""
	public String exchangeNull(String str){
		if(str == null){
			str = "";
		}
		return str;
	}
				
}
